﻿package plugins.mstandio.AS3Interface {
	
	import flash.display.Sprite;	
	import flash.display.StageDisplayState;	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;			
	import flash.system.ApplicationDomain;
	
	import zephyr.BroadcastEvent;
	
	import plugins.mstandio.AS3Interface.buttons.InterfaceButton;
	import plugins.mstandio.AS3Interface.layouts.*;
	import plugins.mstandio.utils.combobox.ComboBox;
	import plugins.mstandio.utils.combobox.ComboBoxEvent;
	
	import plugins.mstandio.Trace.Trace;
		
	/**	 
	 * @author mstandio
	 */
	public class AS3Interface extends Sprite {				
		
		public static const BUTTON_UP:String = "up";
		public static const BUTTON_DOWN:String = "down";
		public static const BUTTON_LEFT:String = "left";
		public static const BUTTON_RIGHT:String = "right";
		public static const BUTTON_ZOOMIN:String = "zoomin";		
		public static const BUTTON_ZOOMOUT:String = "zoomout";		
		public static const BUTTON_AUTOROTATE:String = "autorotate";		
		public static const BUTTON_FULLSCREEN:String = "fullscreen";		
		public static const BUTTON_UNIVERSALMAP:String = "universalmap";
		public static const COMBOBOX:String = "combobox";		
		public static const PARAM_COMBOBOX_VISIBLE:String = "comboboxvisible";		
		public static const PARAM_USE_UNIVERSALMAP:String = "useuniversalmap";		
		
		private var comboBox:ComboBox;
		private var comboBoxVisible:Boolean;
		private var useLoadSpacePlus:Boolean;
		private var useUniversalMap:Boolean;		
		private var spaces:Array;			
		
		private var layoutObject:Object;						
		private var layoutName:String;
		private var layout:Layout;
		
		// classes need to be mentioned
		// comment them out for smaller file
			private var layout1:Layout_default;
			private var layout2:Layout_green;			
			
		private var BroadcastEvent:Class;			
		private var ModuleLoader:Class;				
		private var moduleLoader:Object;		
		private var PanoSalado:Class;		
		private var layerByName:Dictionary;	
		private var panoSalado:Object;			
				
		public function AS3Interface() {						
			if (stage) stageReady();
			else addEventListener(Event.ADDED_TO_STAGE, stageReady, false, 0, true);                                            
		}		
		
		private function stageReady(e:Event=null):void {                                            
            removeEventListener(Event.ADDED_TO_STAGE, stageReady);	
				
			BroadcastEvent = ApplicationDomain.currentDomain.getDefinition("zephyr.BroadcastEvent") as Class;						
			ModuleLoader = ApplicationDomain.currentDomain.getDefinition("ModuleLoader") as Class;                       
            moduleLoader = ModuleLoader(parent); 			
			
			moduleLoader.addEventListener(BroadcastEvent.ALL_LAYERS_LOADED, layersReady, false, 0, true);
			moduleLoader.addEventListener(BroadcastEvent.SPACE_LOADED, updateSelectedItem, false, 0, true);
			moduleLoader.addEventListener(BroadcastEvent.AUTOROTATION_ON, autorotationOn, false, 0, true);
			moduleLoader.addEventListener(BroadcastEvent.AUTOROTATION_OFF, autorotationOff, false, 0, true);				
			moduleLoader.addEventListener(BroadcastEvent.UNIVERSALMAP_CHANGED, universalMapChanged, false, 0, true);
						
			this.stage.addEventListener(Event.RESIZE, this.handleStageResize); 					
		}
		
		
		private function layersReady(e:Event):void {
			PanoSalado = ApplicationDomain.currentDomain.getDefinition("PanoSalado") as Class;			
			layerByName = Dictionary( moduleLoader["layerByName"] );			
			panoSalado = PanoSalado( layerByName["PanoSalado"] );					
			
			this.getSettings();						
			this.buildAll();								
		}
		
		private function updateSelectedItem(e:zephyr.BroadcastEvent):void {						
			if(this.comboBoxVisible){
				var id_processed:String = (String(e.info.spaceLoaded).search(/\.preview$/) == -1) ? 
											String(e.info.spaceLoaded) : 
											String(e.info.spaceLoaded).substring(0, String(e.info.spaceLoaded).lastIndexOf(".preview"));															
			
				for each(var space:Object in spaces) {
					if (space.id == id_processed) {					
						this.comboBox.setSelected(space.label);				
						break;
					}	
				}
			}			
		}	
		
		private function autorotationOn(e:Event=null):void{
			InterfaceButton(this.layoutObject[AS3Interface.BUTTON_AUTOROTATE]).setActive(true);						
		}	
		
		private function autorotationOff(e:Event=null):void{
			InterfaceButton(this.layoutObject[AS3Interface.BUTTON_AUTOROTATE]).setActive(false);			
		}
				
		private function universalMapChanged(e:zephyr.BroadcastEvent):void {
			if(Boolean(e.info.visible)){
				InterfaceButton(this.layoutObject[AS3Interface.BUTTON_UNIVERSALMAP]).setActive(true);
			}else {
				InterfaceButton(this.layoutObject[AS3Interface.BUTTON_UNIVERSALMAP]).setActive(false);
			}
		}			
		
		private function getSettings():void {			
			var settings:XML = moduleLoader.xmlByName["AS3Interface"];			
			this.comboBoxVisible = (settings.@comboBoxVisible != undefined) ?  settings.@comboBoxVisible == "true" : false;			
			this.layoutName = (settings.@layout != undefined) ? String(settings.@layout) : "Layout_default";
			this.useLoadSpacePlus = (settings.@useLoadSpacePlus != undefined) ?  settings.@useLoadSpacePlus == "true" : false;						
			this.useUniversalMap = (settings.@useUniversalMap != undefined) ?  settings.@useUniversalMap == "true" : false;							
			
			var panoSaladoXML:XML = moduleLoader.xmlByName["PanoSalado"] as XML;												
			var spacesList:XMLList = panoSaladoXML.spaces.space.( @id.search(/\.preview$/) == -1 );					
			this.spaces = new Array;						
			for each(var space:XML in spacesList) {								
				this.spaces.push( { label:((space.@label != undefined)? String(space.@label) : String(space.@id)), id:String(space.@id) } );				
			}		
		}				
		
		private function buildAll():void {							
			
			this.comboBox = new ComboBox(this.spaces);
			
			this.layoutObject = new Object();
			
			this.layoutObject[AS3Interface.BUTTON_UP] =  new InterfaceButton(this.upPress, this.upRelease);						
			this.layoutObject[AS3Interface.BUTTON_DOWN] = new InterfaceButton(this.downPress, this.downRelease);				
			this.layoutObject[AS3Interface.BUTTON_LEFT] = new InterfaceButton(this.leftPress, this.leftRelease);				
			this.layoutObject[AS3Interface.BUTTON_RIGHT] = new InterfaceButton(this.rightPress, this.rightRelease);				
			this.layoutObject[AS3Interface.BUTTON_ZOOMIN] = new InterfaceButton(this.zoominPress, this.zoominRelease);						
			this.layoutObject[AS3Interface.BUTTON_ZOOMOUT] = new InterfaceButton(this.zoomoutPress, this.zoomoutRelease);					
			this.layoutObject[AS3Interface.BUTTON_AUTOROTATE] = new InterfaceButton(this.autorotateToggle);					
			this.layoutObject[AS3Interface.BUTTON_FULLSCREEN] = new InterfaceButton(this.fullscreenToggle);												
			this.layoutObject[AS3Interface.BUTTON_UNIVERSALMAP] = new InterfaceButton(this.universalmapToggle);						
			this.layoutObject[AS3Interface.COMBOBOX] = this.comboBox;						
			this.layoutObject[AS3Interface.PARAM_COMBOBOX_VISIBLE] = this.comboBoxVisible; 
			this.layoutObject[AS3Interface.PARAM_USE_UNIVERSALMAP] = this.useUniversalMap;			
																								
			
			var cls:Class = getDefinitionByName("plugins.mstandio.AS3Interface.layouts." + this.layoutName) as Class;						
			try	{
				this.layout = new cls(this.layoutObject);				                      
			}
			catch(e:Error){
				this.layout = new Layout_default(this.layoutObject);   
			}							
			this.layout.addEventListener(ComboBoxEvent.LABEL_CHANGED, comboBoxLabelChanged);						
			this.addChild(this.layout);			
			this.layout.handleStageResize();						
		}		
		
		private function comboBoxLabelChanged(e:ComboBoxEvent):void {					
			if (this.useLoadSpacePlus){
				moduleLoader.dispatchEvent(new BroadcastEvent(BroadcastEvent.LOADSPACEPLUS, {space:e.info.id}));
			}else{
				panoSalado.loadSpace(e.info.id);
			}
		}				
		
		private function handleStageResize(e:Event = null):void {								
			this.layout.handleStageResize();									
			if (this.stage.displayState == StageDisplayState.FULL_SCREEN) {
				this.layoutObject[AS3Interface.BUTTON_FULLSCREEN].setActive(true);
			}else {
				this.layoutObject[AS3Interface.BUTTON_FULLSCREEN].setActive(false);
			}
		}		
		
		// block of functions responsible for basic navigation
		private function autorotateToggle(e:MouseEvent):void {
			panoSalado.execute("toggleAutorotator");
		}
		private function zoominPress(e:MouseEvent):void {
			panoSalado.execute("keyDown:shift");
		}
		private function zoominRelease(e:MouseEvent):void {
			panoSalado.execute("keyUp:shift");
		}		
		private function zoomoutPress(e:MouseEvent):void {
			panoSalado.execute("keyDown:control");
		}
		private function zoomoutRelease(e:MouseEvent):void {
			panoSalado.execute("keyUp:control");
		}		
		private function upPress(e:MouseEvent):void {
			panoSalado.execute("keyDown:up");
		}
		private function upRelease(e:MouseEvent):void {
			panoSalado.execute("keyUp:up");
		}		
		private function downPress(e:MouseEvent):void {
			panoSalado.execute("keyDown:down");
		}
		private function downRelease(e:MouseEvent):void {
			panoSalado.execute("keyUp:down");
		}		
		private function leftPress(e:MouseEvent):void {
			panoSalado.execute("keyDown:left");
		}		
		private function leftRelease(e:MouseEvent):void {
			panoSalado.execute("keyUp:left");
		}		
		private function rightPress(e:MouseEvent):void {
			panoSalado.execute("keyDown:right");
		}
		private function rightRelease(e:MouseEvent):void {
			panoSalado.execute("keyUp:right");
		}		
		private function fullscreenToggle(e:MouseEvent):void {
			panoSalado.execute("toggleFullscreen");
		}						
		private function universalmapToggle(e:MouseEvent):void {						
			moduleLoader.dispatchEvent(new BroadcastEvent(BroadcastEvent.UNIVERSALMAP_TOGGLE));			
		}				
	}	
}